package com.pkk.penguin.exec.pingtai.stragety;

import com.alibaba.fastjson.JSONObject;
import com.pkk.penguin.support.ssh.constands.SSHConstants.DBExecuteType;
import com.pkk.penguin.support.ssh.properties.SSHForwardProperties;
import com.pkk.penguin.support.ssh.strategy.DBExecuteStrategy;
import com.pkk.penguin.support.ssh.util.ResultSetUtil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.MessageFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @description: mysql的执行策略
 * @author: peikunkun
 * @create: 2020-02-27 16:46
 **/
@Slf4j
@Component
public class MysqlExecStragety extends DBExecuteStrategy {

  @Override
  public JSONObject executeSQL(Connection connection, String execSQL) throws SQLException {
    Statement statement = null;
    PreparedStatement preparedStatement = null;
    ResultSet rs = null;
    JSONObject result = new JSONObject();
    try {
      statement = connection.createStatement();
      final DBExecuteType dbExecuteType = super.execSQLType(execSQL);
      if (DBExecuteType.QUERY.equals(dbExecuteType)) {
        rs = statement.executeQuery(execSQL);
        result.put("result", ResultSetUtil.toListMap(rs));
        return result;
      } else if (DBExecuteType.UPDATE.equals(dbExecuteType)) {
        preparedStatement = connection.prepareStatement(execSQL);
        result.put("result", statement.executeUpdate(execSQL));
        return result;
      } else if (DBExecuteType.ALERT.equals(dbExecuteType)) {
        preparedStatement = connection.prepareStatement(execSQL);
        result.put("result", statement.executeUpdate(execSQL));
        return result;
      }
      return result;
    } catch (SQLException e) {
      throw e;
    } finally {
      this.manualClose(preparedStatement, rs, statement);
    }
  }

  @Override
  public Connection getConnection(String driveClass, String url, String userName, String userPwd) {
    try {
      Class.forName(driveClass);
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
    Connection connection = null;
    try {
      connection = DriverManager.getConnection(url, userName, userPwd);
    } catch (SQLException e) {
      e.printStackTrace();
      return null;
    }
    return connection;
  }

  @Override
  public void manualClose(Object... objects) {
    if (null != objects[0]) {
      try {
        ((PreparedStatement) objects[0]).close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }

    if (null != objects[1]) {
      try {
        ((ResultSet) objects[1]).close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }

    if (null != objects[2]) {
      try {
        ((Statement) objects[2]).close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }


  @Override
  public String getDbLink(SSHForwardProperties dbProperties, String forwardLocalPort) {
    return MessageFormat.format(dbProperties.getDbLinkTemplate(), forwardLocalPort, dbProperties.getDbName());
  }
}
